<?php
/**
 * process
 * 工作流步骤
 * @author Administrator
 * @version
 */
class Model_Tables_Process extends Zend_Db_Table_Abstract {
    /**
     * The default table name
     */
    protected $_name = 'process';
    /**
     * add
     * 增加工作流步骤
     * @param $params 字段信息
     */
    public function add( $params )
    {
        $data = array(
		'workflowid' => $params['workflowid'],
		'name' => $params['name'],
		'description' => $params['description'],
		'passname' => $params['passname'],
		'passstatus' => $params['passstatus'],
		'rejectname' => $params['rejectname'],
		'rejectstatus' => $params['rejectstatus'],
        );
        $db = $this->getAdapter();
        $db->beginTransaction();
    	try {
    		$this->insert($data);			
			$processid = $db->lastInsertId();
			$status = $params['status'];
			$sqlinsert = "insert into process_status (processid, status) values	($processid, ?)";
			foreach($status as $s)
			{
			    $db->query($db->quoteInto($sqlinsert,$s));
			}
			$db->commit();
		} catch (Exception $e) {
			print_r($e) ;
			$db->rollBack();
			return false;
		}
    }
    /**
     * modify
     * 编辑工作流步骤
     * @param $params 字段信息
     * @param $processid 主键值
     */
    public function modify( $params, $processid )
    {
        $data = array(
		'workflowid' => $params['workflowid'],
		'name' => $params['name'],
		'description' => $params['description'],
		'passname' => $params['passname'],
		'passstatus' => $params['passstatus'],
		'rejectname' => $params['rejectname'],
		'rejectstatus' => $params['rejectstatus'],
        );
        $status = $params['status'];
        $db = $this->getAdapter();
        $db->beginTransaction();
    	try {
    		$where = $db->quoteInto('processid = ?',$processid);
    		$this->update($data, $where);
			$sqldelete = $db->quoteInto("delete from process_status where processid = ?",$processid);
			$sqlinsert = "insert into process_status (processid, status) values	($processid, ?)";
			$db->query($sqldelete);//删除原来的值然后插入新值
			foreach($status as $s)
			{
			    $db->query($db->quoteInto($sqlinsert,$s));
			}
			$db->commit();
		} catch (Exception $e) {
			$db->rollBack();
			return false;
		}
    }
    /**
     * modifyAjax
     * 无刷新编辑工作流步骤
     * @param $processid 主键值
     * @param $field 字段
     * @param $value 值
     */
    public function modifyAjax( $processid, $field, $value )
    {
    	$db = $this->getAdapter();
    	$data = array($field => $value);
    	$where = $db->quoteInto("processid=?", $processid);
        return $this->update($data , $where );
    }
    /**
     * modifyAjaxRevers
     * 无刷新字段反转工作流步骤
     * @param $field 需要反转的字段
     * @param $processid 主键值
     */
    public function modifyAjaxRevers( $field ,$pri)
    {
    	$db = $this->getAdapter();
    	$sql = "update process set  $field =not  $field  where processid=' $pri'";
		return $db->query($sql);
    }
    /**
     * del
     * 根据条件删除工作流步骤
     * @param $processid 主键值
     */
    public function del( $processid )
    {
     	$where = 'processid = '.$processid;
        return $this->delete($where);
    }
    /**
     * getInfo
     * 获取工作流步骤信息
     * @param $processid 主键值
     * @return 结果数组
     */
    public function getInfo($processid)
    {
        $row = $this->fetchRow('processid = ' . $processid);
        if (!$row) {
        	return null;
        }
        else
        {
        	$result = $row->toArray();
        	
        	return $result;
        }
    }
    /**
     * getCount
     * 获取工作流步骤数量
     * @param int  搜索条件
     * @return 结果数
     */
    public function getCount($s = "")
    { 
        $select = $this->select()->from($this->_name,'count(*) as num');
    	if($s!="")  
    	{
    		$db = $this->getAdapter();
			$where = $db->quoteInto("name like ?", "%$s%");
    		$select = $select->where($where);
    	}
        $result = $this->fetchRow($select)->num;
        return $result;
    }
    /**
     * getList
     * 取工作流步骤数据分页
     * @param int  页码
     * @param int  每页的行数 
     * @param  搜索条件
     * @return 行数组
     */
    public function getList($page, $numPerPage = 10,$s= "",$workflowid=0)
    {
    	$db = $this->getAdapter();   	
    	$page = (1>$page) ? 1 : $page;
    	$offset = ($page-1)*$numPerPage;
        $orderby = array('processid');
        $where = $db->quoteInto("workflowid= ?", $workflowid);
        if($s!="")     
		{
			$where.= $db->quoteInto(" and name like ?", "%$s%");
		}  
        $result = $this->fetchAll($where,$orderby, $numPerPage, $offset )->toArray();
        return $result;
    }
    /*
     * 获取步骤名称列表
     */
 	public function getStatusList()
    {
        $db = $this->getAdapter();		
		$sql = "select status, name from status";
		$result = $db->query($sql);
    	$status = array();
        if (!empty($result))
	        {
	            foreach ($result as $item)
	            {
	                $status[$item['status']] = $item['name'];
	            }
	        }
        return $status;
    }
}
